package com.nhc.activity_fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;

import com.astrob.nhcmap.R;

import zuo.biao.library.ui.EditTextInfoWindow;


public class ListOrderCtrlActivity extends Activity implements View.OnClickListener {
    private final String TAG = "ListOrderCtrlActivity";

    //启动方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    public static final String INTENT_LISTORDER_MAX = "INTENT_LISTORDER_MAX";
    public static final String INTENT_LISTORDER_CUR = "INTENT_LISTORDER_CUR";

    /**
     * 启动这个Activity的Intent
     *
     * @param context
     * @return
     */

    public static Intent createIntent(Context context, int orderThis, int orderMax) {
        return new Intent(context, ListOrderCtrlActivity.class)
                .putExtra(INTENT_LISTORDER_CUR, orderThis)
                .putExtra(INTENT_LISTORDER_MAX, orderMax);
    }


    //启动方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    int _orderMax = 0;//列表最大index
    int _orderThis = 0;//当前index  0开始

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.listorderctrl_activity);

        _orderMax = getIntent().getIntExtra(INTENT_LISTORDER_MAX, _orderMax);
        _orderThis = getIntent().getIntExtra(INTENT_LISTORDER_CUR, _orderThis);

        //功能归类分区方法，必须调用<<<<<<<<<<
        initView();
        initData();
        initEvent();
        //功能归类分区方法，必须调用>>>>>>>>>>

    }

    //UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    private Button bt_orderitem_4;

    public void initView() {//必须调用
        bt_orderitem_4 = (Button) findViewById(R.id.bt_orderitem_4);
    }

    //UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    //Data数据区(存在数据获取或处理代码，但不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    public void initData() {//必须调用
        bt_orderitem_4.setText(getString(R.string.input_xxoo) + _orderMax + ")");
    }

    //Data数据区(存在数据获取或处理代码，但不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //Event事件区(只要存在事件监听代码就是)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    public void initEvent() {//必须调用
        findViewById(R.id.bt_orderitem_0).setOnClickListener(this);
        findViewById(R.id.bt_orderitem_1).setOnClickListener(this);
        findViewById(R.id.bt_orderitem_2).setOnClickListener(this);
        findViewById(R.id.bt_orderitem_3).setOnClickListener(this);
        findViewById(R.id.bt_orderitem_4).setOnClickListener(this);
        findViewById(R.id.bt_orderitem_5).setOnClickListener(this);
    }
    //系统自带监听方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


    //类相关监听<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    @Override
    public void finish() {
        super.finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_orderitem_0:
                _orderThis--;
                doOrder(_orderThis);
                break;
            case R.id.bt_orderitem_1:
                _orderThis++;
                doOrder(_orderThis);
                break;
            case R.id.bt_orderitem_2:
                doOrder(0);
                break;
            case R.id.bt_orderitem_3:
                doOrder(_orderMax - 1);
                break;
            case R.id.bt_orderitem_4:
                onInputOrder();
                break;
            case R.id.bt_orderitem_5:
                doOrder(-1);
                break;
            default:
                break;
        }
    }

    private void doOrder(int order) {
        setResult(order);
        finish();
    }

    private void onInputOrder() {
        Intent intent = EditNumberActivity.createIntent(this, getString(R.string.input_order), _orderThis+1, "");
        startActivityForResult(intent, REQUEST_TO_EDIT_ORDER);
    }

    private final int REQUEST_TO_EDIT_ORDER = 23;

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == REQUEST_TO_EDIT_ORDER) {
            if (RESULT_OK == resultCode && data != null) {
                String editedValue = data.getStringExtra(EditTextInfoWindow.RESULT_VALUE);
                int order = Integer.valueOf(editedValue);
                if (order < 1 || order > _orderMax) {
                    Toast.makeText(this, getString(R.string.input_xxx) + _orderMax + ")", Toast.LENGTH_SHORT).show();
                    return;
                }
                order--;
                setResult(order);
                finish();
            }
        }

    }

    //类相关监听>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    //系统自带监听方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //Event事件区(只要存在事件监听代码就是)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


}
